package com.cmo.rzl.shine.controller;

import java.util.List;

import com.cmo.rzl.core.dao.Db;
import com.cmo.rzl.core.jfinal.ext.kit.JsonKit;
import com.cmo.rzl.core.model.AjaxResult;
import com.cmo.rzl.core.toolbox.Record;
import com.cmo.rzl.core.toolbox.kit.ShardKit;
import com.cmo.rzl.shine.model.Terminal;
import com.cmo.rzl.shine.service.QueueTypeService;
import com.cmo.rzl.shine.service.TerminalService;
import com.cmo.rzl.shine.service.impl.QueueTypeServiceImpl;
import com.cmo.rzl.shine.service.impl.TerminalServiceImpl;
import com.cmo.rzl.system.controller.base.UrlPermissController;

/**
 * Generated by Blade.
 * 2017-08-12 15:10:04
 */
public class TerminalController extends UrlPermissController {
	private static String CODE = "terminal";
	private static String PERFIX = "terminal";
	private static String LIST_SOURCE = "Terminal.list";
	private static String BASE_PATH = "/platform/terminal/";
	
	TerminalService service = new TerminalServiceImpl();
	QueueTypeService qservice = new QueueTypeServiceImpl();
	ShardKit shard=new ShardKit();
	
	public void index() {
		setAttr("code", CODE);
		render(BASE_PATH + "terminal.html");
	}

	public void add() {
		setAttr("code", CODE);
		List<Record> list_triageName = qservice.queryTriageName();
		setAttr("list_triageName", list_triageName);
		render(BASE_PATH + "terminal_add.html");
	}

	public void edit() {
		String id = getPara(0);
		List<Record> list_triageName = qservice.queryTriageName();
		setAttr("list_triageName", list_triageName);
		Terminal terminal = service.findById(id);
		setAttr("model", JsonKit.toJson(terminal));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "terminal_edit.html");
	}

	public void view() {
		String id = getPara(0);
		Terminal terminal = service.findById(id);
		setAttr("model", JsonKit.toJson(terminal));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "terminal_view.html");
	}

	public void list() {
		Object grid = paginate(LIST_SOURCE);
		renderJson(grid);
	}

	public void save() {
		Terminal terminal = mapping(PERFIX, Terminal.class);
		if(terminal.getTriage_id()==null){
			renderJson(error("请先添加分诊台"));
			return;
		}
		String ip= terminal.getIp();
		if(shard.isIP(ip)==false){
			renderJson(error("请输入正确的ip地址"));
			return;
		}
		boolean temp = service.save(terminal);
		if (temp) {
			renderJson(success(SAVE_SUCCESS_MSG));
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}

	public void update() {
		Terminal terminal = mapping(PERFIX, Terminal.class);
		if(terminal.getTriage_id()==null){
			renderJson(error("请先添加分诊台"));
			return;
		}
		String ip= terminal.getIp();
		if(shard.isIP(ip)==false){
			renderJson(error("请输入正确的ip地址"));
			return;
		}
		boolean temp = service.update(terminal);
		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

	public void remove() {
		String ids = getPara("ids");
		int cnt = service.deleteByIds(ids);
		if (cnt > 0) {
			renderJson(success(DEL_SUCCESS_MSG));
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}
	
	public void select_pagerterminal() {
		int type = getParaToInt("type", 1);
		int aid = getParaToInt("aid", 0);
		
		String sql="";
		// 已选择
		if (type == 1) {
			sql+="SELECT b.id,b.name as tername,b.display_name from rlt_pager2terminal a INNER JOIN terminal b ON a.id=b.id INNER JOIN pager c ON "
					+ "c.id=a.pager_id INNER JOIN triage d ON d.triage_id=c.triage_id where c.id="+aid+"";
		} else {// 未选择
			String sql1="SELECT b.id from rlt_pager2terminal a INNER JOIN terminal b ON a.id=b.id INNER JOIN pager c ON "
					+ "c.id=a.pager_id INNER JOIN triage d ON d.triage_id=c.triage_id where c.id="+aid+"";
			List<Record> list=Db.init().selectList(sql1);
			String str="";
			if(!list.isEmpty()){
				for(int i=0;i<list.size();i++){
					str+=list.get(i).getInt("id").toString()+",";
				}
				str=str.substring(0,str.length()-1);
			}
			
			System.out.println(str+"*****************");
			sql += "SELECT a.id,a.name as tername,a.display_name FROM terminal a INNER JOIN triage b ON a.triage_id = b.triage_id INNER JOIN pager c "
					+ "ON c.triage_id = b.triage_id WHERE c.id="+aid+"";
			if(!"".equals(str)){
				sql+=" AND a.id NOT IN ("+str+")";
			}
		}

		Object grid = paginate(sql);

		renderJson(grid);
	}

	public void updateterminalChild() {

		int type = getParaToInt("type", 1);
		int aid = getParaToInt("aid");
		System.out.println(aid+"叫号器id*******************");
		int gid = getParaToInt("gid");
		System.out.println(gid+"屏id************************");

		Record record = Record.create().set("aid", aid).set("gid", gid);
		AjaxResult result = new AjaxResult();

		result.setCode(0);
		if (type == 1) {
			String sql = "INSERT INTO rlt_pager2terminal VALUES("+aid+","+gid+")";
			Db.init().update(sql, record);
			result.setMessage("添加成功");
		} else {
			String sql = "DELETE FROM rlt_pager2terminal WHERE pager_id="+aid+" AND id="+gid+"";
			record.remove("aid");
			Db.init().update(sql, record);
			result.setMessage("移除成功");
		}
		renderJson(result);
	}
}
